/**
 * Copyright (c) 2017-2017 All Rights Reserved.
 */
package com.zbest.zpay.exchange.task;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zbest.zpay.exchange.dal.dao.BatchInfoDAO;
import com.zbest.zpay.exchange.dal.model.BatchInfoDO;
import com.zbest.zpay.exchange.helper.SeqHelper;
import com.zbest.zpay.exchange.integration.gw.GwClient;
import com.zbest.zpay.exchange.model.BatchStatus;
import com.zbest.zpay.masterdata.cache.ChannelConfigCache;
import com.zbest.zpay.masterdata.dal.model.ChannelConfigDO;
import com.zbest.zpay.masterdata.model.TransType;
import com.zbest.zpay.service.gw.model.GwKeyEnum;
import com.zbest.zpay.service.gw.model.GwResult;
import com.zbest.zpay.service.gw.model.InstStatus;

/**
 * 业务对账文件下载
 *
 * @author Administrator
 * @version $Id: BizReconFileDownload.java, v 0.1 2017-4-16 下午6:14:56 Administrator Exp $
 */
public class BizReconFileDownloadTask {

    private static final Logger logger = LoggerFactory.getLogger(BizReconFileDownloadTask.class);

    @Resource
    private ChannelConfigCache  channelConfigCache;

    @Resource
    private String              payChannelId;

    @Resource
    private GwClient            gwClient;

    @Resource
    private BatchInfoDAO        batchInfoDAO;

    @Resource
    private SeqHelper           seqHelper;

    public void execute() {
        ChannelConfigDO channelConfigDO = channelConfigCache.getChannelId(payChannelId,
            TransType.BIZFILEDOWNLOAD.name());
        //FIXME 看下文件下载需要哪些参数
        GwResult gwResult = gwClient.syncSend(channelConfigDO.getChannelId(), null);
        if (gwResult.isSuccess() && gwResult.getInstStatus().equals(InstStatus.SUCCESS.getCode())) {
            BatchInfoDO batchInfoDO = new BatchInfoDO();
            batchInfoDO.setBatchNo(seqHelper.genBatchNo());
            batchInfoDO.setChannelId(payChannelId);
            String filePath = (String) gwResult.getResultData().get(GwKeyEnum.FILE_PATH.getCode());
            batchInfoDO.setFilePath(filePath);
            batchInfoDO.setStatus(BatchStatus.DOWNLOAD.getCode());
            batchInfoDAO.insert(batchInfoDO);
        } else {
            logger.error("文件下载异常gwResult=" + gwResult);
        }
    }
}
